rsubmit;
data data1; set comp.co_afnd1 (keep = gvkey datadate
dltt ceq csho dlc);	proc sort; by gvkey datadate;
data data2; set comp.co_afnd2 (keep = gvkey datadate
txdb); proc sort; by gvkey datadate;
data price; set comp.co_amkt (keep = gvkey datadate
cfflag prcc); where cfflag='F'; proc sort; by gvkey datadate;
data year; set comp.co_adesind (keep = gvkey datadate
fyear fyr);	proc sort; by gvkey datadate;
data sic; set comp.company (keep = gvkey sic); proc sort; by gvkey;
data cusip; set comp.security (keep = gvkey cusip); proc sort; by gvkey;
data compann; merge data1 data2 year price; by gvkey datadate;
data compann; merge compann sic cusip; by gvkey;
if (4900<=sic<5000 or 6000<=sic<7000) then delete;
year=fyear; if 0<fyr<5 then year=year+1;
if year<1961 or year>2017 then delete;
tdebt=dltt+dlc; mve=csho*prcc; mlev=tdebt/(tdebt+mve);
if tdebt+mve<=0 then mlev=.; if nmiss(txdb) then txdb=0;
cusip=substr(cusip,1,8); bvalue=ceq+txdb; mb=mve/bvalue;
if mb<=0 then mb=.; if nmiss(mlev) and nmiss(mb) then delete;
keep cusip year mlev mb; proc sort data=compann nodupkey;
by cusip year; proc means; run;

rsubmit;
data compf; set comp.funda (keep = gvkey cusip datadate sich
dltt csho dlc ceq prcc_f datafmt indfmt popsrc consol);
where indfmt='INDL' and datafmt='STD' and popsrc='D' and consol='C';
if (4900<=sich<5000 or 6000<=sich<7000) then delete;
year=year(datadate); month=month(datadate);
if month>5 then year=year+1; if year<1961 or year>2017 then delete;
tdebt=dltt+dlc; mve=csho*prcc_f; mlev=tdebt/(tdebt+mve);
if tdebt+mve<=0 then mlev=.; if nmiss(txdb) then txdb=0;
cusip=substr(cusip,1,8); bvalue=ceq+txdb; mb=mve/bvalue;
if mb<=0 then mb=.; if nmiss(mb) then delete; keep cusip gvkey year mlev mb; 
proc sort data=compf nodupkey; by cusip year; proc means; run;

rsubmit;
options source notes errors=5;
data ivol; merge temp.crsp temp.ivol;
by cusip year month; proc sort; by year month;
proc univariate data=ivol noprint;
var ivol; by year month; where exchcd=1;
output out=decile pctlpts = 20 to 80 by 20 pctlpre=dec;
DATA ivol; MERGE ivol decile; BY year month;
  pdecile=1;
  IF ivol > DEC20 THEN pdecile=2;
  IF ivol > DEC40 THEN pdecile=3;
  IF ivol > DEC60 THEN pdecile=4;
  IF ivol > DEC80 THEN pdecile=5;
  if nmiss(ivol) then pdecile=.;
drop dec20 dec40 dec60 dec80;
proc sort data=ivol; by cusip year month;
data ivol; set ivol; lme=lag(me);
lcusip=lag(cusip); ivoldecile=lag(pdecile);
if lcusip ne cusip then ivoldecile=.;
if lcusip ne cusip then lme=.; proc means; run;

data opt; set compann; year=year+1; proc sort nodupkey; by cusip year;
data cap; set temp.crsp (keep = cusip year month exchcd price);
where month=12; year=year+1; drop month; proc sort nodupkey; by cusip year;
data opt; merge cap opt; by cusip year; proc sort; by year; run;
proc univariate data=opt noprint; var mb; by year; where exchcd=1;
output out=decile pctlpts = 20 to 80 by 20 pctlpre=dec;
DATA opt; MERGE opt decile; BY year;
  mbdecile=1;
  IF mb > DEC20 THEN mbdecile=2;
  IF mb > DEC40 THEN mbdecile=3;
  IF mb > DEC60 THEN mbdecile=4;
  IF mb > DEC80 THEN mbdecile=5;
  if nmiss(mb) then mbdecile=.;
drop dec20 dec40 dec60 dec80; proc means; run;
proc sort; by cusip year;
data ivol; merge ivol opt; by cusip year;

proc sort data=ivol nodupkey;
by cusip year month ivol;
proc tabulate data=ivol format=5.3;
class ivoldecile; var return;
table return, (ivoldecile all)*mean;
proc tabulate data=ivol format=5.3;
class mbdecile; var return;
table return, (mbdecile all)*mean; run;

options nosource nonotes errors=0;
proc sort data=ivol; by year month mbdecile ivoldecile;
proc means data=ivol noprint;
var return; by year month mbdecile ivoldecile;
weight lme; output out=factor mean(return) = port;
%macro a(many); %do i=1 %to 5; %do j=1 %to 5;
data factor&i&j; set factor; where ivoldecile=&i and mbdecile=&j;
port&i&j=port; drop port ivoldecile mbdecile;
run; %end; %end; %mend a; %a(1);
data temp.factor;
merge factor11 factor12 factor13 factor14 factor15
factor21 factor22 factor23 factor24 factor25
factor31 factor32 factor33 factor34 factor35
factor41 factor42 factor43 factor44 factor45
factor51 factor52 factor53 factor54 factor55;
by year month; if nmiss(month) then delete;
if (year=1963 and 1<month<7) or year>2017 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls"
DBMS=EXCEL REPLACE; SHEET="mbvw"; RUN;
rsubmit;
proc sort data=ivol; by year month mbdecile ivoldecile;
proc means data=ivol noprint;
var return; by year month mbdecile ivoldecile;
output out=factor mean(return) = port;
%macro a(many); %do i=1 %to 5; %do j=1 %to 5;
data factor&i&j; set factor; where ivoldecile=&i and mbdecile=&j;
port&i&j=port; drop port ivoldecile mbdecile;
run; %end; %end; %mend a; %a(1);
data temp.factor;
merge factor11 factor12 factor13 factor14 factor15
factor21 factor22 factor23 factor24 factor25
factor31 factor32 factor33 factor34 factor35
factor41 factor42 factor43 factor44 factor45
factor51 factor52 factor53 factor54 factor55;
by year month; if nmiss(month) then delete;
if (year=1963 and 1<month<7) or year>2017 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls"
DBMS=EXCEL REPLACE; SHEET="mbew"; RUN;

rsubmit;
options source notes errors=5;
data ivol; merge temp.crsp temp.ivol;
by cusip year month; proc sort; by year month;
proc univariate data=ivol noprint;
var ivol; by year month; where exchcd=1 and price ge 5;
output out=decile pctlpts = 20 to 80 by 20 pctlpre=dec;
DATA ivol; MERGE ivol decile; BY year month;
  pdecile=1;
  IF ivol > DEC20 THEN pdecile=2;
  IF ivol > DEC40 THEN pdecile=3;
  IF ivol > DEC60 THEN pdecile=4;
  IF ivol > DEC80 THEN pdecile=5;
  if nmiss(ivol) or price<5 then pdecile=.;
drop dec20 dec40 dec60 dec80;
proc sort data=ivol; by cusip year month;
data ivol; set ivol; lme=lag(me);
lcusip=lag(cusip); ivoldecile=lag(pdecile);
if lcusip ne cusip then ivoldecile=.;
if lcusip ne cusip then lme=.; proc means; run;

data opt; set compann; year=year+1; proc sort nodupkey; by cusip year;
data cap; set temp.crsp (keep = cusip year month exchcd price);
where month=12; year=year+1; drop month; proc sort nodupkey; by cusip year;
data opt; merge cap opt; by cusip year; proc sort; by year; run;
proc univariate data=opt noprint; var mb; by year; where exchcd=1 and price ge 5;
output out=decile pctlpts = 20 to 80 by 20 pctlpre=dec;
DATA opt; MERGE opt decile; BY year;
  mbdecile=1;
  IF mb > DEC20 THEN mbdecile=2;
  IF mb > DEC40 THEN mbdecile=3;
  IF mb > DEC60 THEN mbdecile=4;
  IF mb > DEC80 THEN mbdecile=5;
  if nmiss(mb) or price<5 then mbdecile=.;
drop dec20 dec40 dec60 dec80; proc means; run;
proc sort; by cusip year;
data ivol; merge ivol opt; by cusip year;

proc sort data=ivol nodupkey;
by cusip year month ivol;
proc tabulate data=ivol format=5.3;
class ivoldecile; var return;
table return, (ivoldecile all)*mean;
proc tabulate data=ivol format=5.3;
class mbdecile; var return;
table return, (mbdecile all)*mean; run;

options nosource nonotes errors=0;
proc sort data=ivol; by year month mbdecile ivoldecile;
proc means data=ivol noprint;
var return; by year month mbdecile ivoldecile;
weight lme; output out=factor mean(return) = port;
%macro a(many); %do i=1 %to 5; %do j=1 %to 5;
data factor&i&j; set factor; where ivoldecile=&i and mbdecile=&j;
port&i&j=port; drop port ivoldecile mbdecile;
run; %end; %end; %mend a; %a(1);
data temp.factor;
merge factor11 factor12 factor13 factor14 factor15
factor21 factor22 factor23 factor24 factor25
factor31 factor32 factor33 factor34 factor35
factor41 factor42 factor43 factor44 factor45
factor51 factor52 factor53 factor54 factor55;
by year month; if nmiss(month) then delete;
if (year=1963 and 1<month<7) or year>2017 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls"
DBMS=EXCEL REPLACE; SHEET="mbvw5"; RUN;
rsubmit;
proc sort data=ivol; by year month mbdecile ivoldecile;
proc means data=ivol noprint;
var return; by year month mbdecile ivoldecile;
output out=factor mean(return) = port;
%macro a(many); %do i=1 %to 5; %do j=1 %to 5;
data factor&i&j; set factor; where ivoldecile=&i and mbdecile=&j;
port&i&j=port; drop port ivoldecile mbdecile;
run; %end; %end; %mend a; %a(1);
data temp.factor;
merge factor11 factor12 factor13 factor14 factor15
factor21 factor22 factor23 factor24 factor25
factor31 factor32 factor33 factor34 factor35
factor41 factor42 factor43 factor44 factor45
factor51 factor52 factor53 factor54 factor55;
by year month; if nmiss(month) then delete;
if (year=1963 and 1<month<7) or year>2017 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls"
DBMS=EXCEL REPLACE; SHEET="mbew5"; RUN;

rsubmit;
options source notes errors=5;
data ivol; merge temp.crsp temp.ivol;
by cusip year month; proc sort; by year month;
proc univariate data=ivol noprint;
var ivol; by year month; where exchcd=1;
output out=decile pctlpts = 20 to 80 by 20 pctlpre=dec;
DATA ivol; MERGE ivol decile; BY year month;
  pdecile=1;
  IF ivol > DEC20 THEN pdecile=2;
  IF ivol > DEC40 THEN pdecile=3;
  IF ivol > DEC60 THEN pdecile=4;
  IF ivol > DEC80 THEN pdecile=5;
  if nmiss(ivol) then pdecile=.;
drop dec20 dec40 dec60 dec80;
proc sort data=ivol; by cusip year month;
data ivol; set ivol; lme=lag(me);
lcusip=lag(cusip); ivoldecile=lag(pdecile);
if lcusip ne cusip then ivoldecile=.;
if lcusip ne cusip then lme=.; proc means; run;

data opt; set compf; year=year+1; proc sort nodupkey; by cusip year;
data cap; set temp.crsp (keep = cusip year month exchcd);
where month=12; year=year+1; drop month; proc sort nodupkey; by cusip year;
data opt; merge cap opt; by cusip year; proc sort; by year; run;
proc univariate data=opt noprint; var mb; by year; where exchcd=1;
output out=decile pctlpts = 20 to 80 by 20 pctlpre=dec;
DATA opt; MERGE opt decile; BY year;
  mbdecile=1;
  IF mb > DEC20 THEN mbdecile=2;
  IF mb > DEC40 THEN mbdecile=3;
  IF mb > DEC60 THEN mbdecile=4;
  IF mb > DEC80 THEN mbdecile=5;
  if nmiss(mb) then mbdecile=.;
drop dec20 dec40 dec60 dec80; proc means; run;
proc sort; by cusip year;
data ivol; merge ivol opt; by cusip year;

proc sort data=ivol nodupkey;
by cusip year month ivol;
proc tabulate data=ivol format=5.3;
class ivoldecile; var return;
table return, (ivoldecile all)*mean;
proc tabulate data=ivol format=5.3;
class mbdecile; var return;
table return, (mbdecile all)*mean; run;

options nosource nonotes errors=0;
proc sort data=ivol; by year month mbdecile ivoldecile;
proc means data=ivol noprint;
var return; by year month mbdecile ivoldecile;
weight lme; output out=factor mean(return) = port;
%macro a(many); %do i=1 %to 5; %do j=1 %to 5;
data factor&i&j; set factor; where ivoldecile=&i and mbdecile=&j;
port&i&j=port; drop port ivoldecile mbdecile;
run; %end; %end; %mend a; %a(1);
data temp.factor;
merge factor11 factor12 factor13 factor14 factor15
factor21 factor22 factor23 factor24 factor25
factor31 factor32 factor33 factor34 factor35
factor41 factor42 factor43 factor44 factor45
factor51 factor52 factor53 factor54 factor55;
by year month; if nmiss(month) then delete;
if year<1964 or year>2017 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls"
DBMS=EXCEL REPLACE; SHEET="mbfvw"; RUN;
rsubmit;
proc sort data=ivol; by year month mbdecile ivoldecile;
proc means data=ivol noprint;
var return; by year month mbdecile ivoldecile;
output out=factor mean(return) = port;
%macro a(many); %do i=1 %to 5; %do j=1 %to 5;
data factor&i&j; set factor; where ivoldecile=&i and mbdecile=&j;
port&i&j=port; drop port ivoldecile mbdecile;
run; %end; %end; %mend a; %a(1);
data temp.factor;
merge factor11 factor12 factor13 factor14 factor15
factor21 factor22 factor23 factor24 factor25
factor31 factor32 factor33 factor34 factor35
factor41 factor42 factor43 factor44 factor45
factor51 factor52 factor53 factor54 factor55;
by year month; if nmiss(month) then delete;
if year<1964 or year>2017 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls"
DBMS=EXCEL REPLACE; SHEET="mbfew"; RUN;

rsubmit;
options source notes errors=5;
data ivol; merge temp.crsp temp.ivol;
by cusip year month; proc sort; by year month;
proc univariate data=ivol noprint;
var ivol; by year month; where exchcd=1 and price ge 5;
output out=decile pctlpts = 20 to 80 by 20 pctlpre=dec;
DATA ivol; MERGE ivol decile; BY year month;
  pdecile=1;
  IF ivol > DEC20 THEN pdecile=2;
  IF ivol > DEC40 THEN pdecile=3;
  IF ivol > DEC60 THEN pdecile=4;
  IF ivol > DEC80 THEN pdecile=5;
  if nmiss(ivol) or price<5 then pdecile=.;
drop dec20 dec40 dec60 dec80;
proc sort data=ivol; by cusip year month;
data ivol; set ivol; lme=lag(me);
lcusip=lag(cusip); ivoldecile=lag(pdecile);
if lcusip ne cusip then ivoldecile=.;
if lcusip ne cusip then lme=.; proc means; run;

data opt; set compf; year=year+1; proc sort nodupkey; by cusip year;
data cap; set temp.crsp (keep = cusip year month exchcd price);
where month=12; year=year+1; drop month; proc sort nodupkey; by cusip year;
data opt; merge cap opt; by cusip year; proc sort; by year; run;
proc univariate data=opt noprint; var mb; by year; where exchcd=1 and price ge 5;
output out=decile pctlpts = 20 to 80 by 20 pctlpre=dec;
DATA opt; MERGE opt decile; BY year;
  mbdecile=1;
  IF mb > DEC20 THEN mbdecile=2;
  IF mb > DEC40 THEN mbdecile=3;
  IF mb > DEC60 THEN mbdecile=4;
  IF mb > DEC80 THEN mbdecile=5;
  if nmiss(mb) or price<5 then mbdecile=.;
drop dec20 dec40 dec60 dec80; proc means; run;
proc sort; by cusip year;
data ivol; merge ivol opt; by cusip year;

proc sort data=ivol nodupkey;
by cusip year month ivol;
proc tabulate data=ivol format=5.3;
class ivoldecile; var return;
table return, (ivoldecile all)*mean;
proc tabulate data=ivol format=5.3;
class mbdecile; var return;
table return, (mbdecile all)*mean; run;

options nosource nonotes errors=0;
proc sort data=ivol; by year month mbdecile ivoldecile;
proc means data=ivol noprint;
var return; by year month mbdecile ivoldecile;
weight lme; output out=factor mean(return) = port;
%macro a(many); %do i=1 %to 5; %do j=1 %to 5;
data factor&i&j; set factor; where ivoldecile=&i and mbdecile=&j;
port&i&j=port; drop port ivoldecile mbdecile;
run; %end; %end; %mend a; %a(1);
data temp.factor;
merge factor11 factor12 factor13 factor14 factor15
factor21 factor22 factor23 factor24 factor25
factor31 factor32 factor33 factor34 factor35
factor41 factor42 factor43 factor44 factor45
factor51 factor52 factor53 factor54 factor55;
by year month; if nmiss(month) then delete;
if year<1964 or year>2017 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls"
DBMS=EXCEL REPLACE; SHEET="mbfvw5"; RUN;
rsubmit;
proc sort data=ivol; by year month mbdecile ivoldecile;
proc means data=ivol noprint;
var return; by year month mbdecile ivoldecile;
output out=factor mean(return) = port;
%macro a(many); %do i=1 %to 5; %do j=1 %to 5;
data factor&i&j; set factor; where ivoldecile=&i and mbdecile=&j;
port&i&j=port; drop port ivoldecile mbdecile;
run; %end; %end; %mend a; %a(1);
data temp.factor;
merge factor11 factor12 factor13 factor14 factor15
factor21 factor22 factor23 factor24 factor25
factor31 factor32 factor33 factor34 factor35
factor41 factor42 factor43 factor44 factor45
factor51 factor52 factor53 factor54 factor55;
by year month; if nmiss(month) then delete;
if year<1964 or year>2017 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls"
DBMS=EXCEL REPLACE; SHEET="mbfew5"; RUN;

rsubmit;
options source notes errors=5;
data ivol; merge temp.crsp temp.ivol; by cusip year month;
data opt; set compann; year=year+1; proc sort nodupkey; by cusip year;
data cap; set temp.crsp (keep = cusip year month exchcd price);
where month=12; year=year+1; drop month; proc sort nodupkey; by cusip year;
data opt; merge cap opt; by cusip year; proc sort; by year; run;
proc univariate data=opt noprint; var mb; by year; where exchcd=1;
output out=decile pctlpts = 20 to 80 by 20 pctlpre=dec;
DATA opt; MERGE opt decile; BY year;
  mbdecile=1;
  IF mb > DEC20 THEN mbdecile=2;
  IF mb > DEC40 THEN mbdecile=3;
  IF mb > DEC60 THEN mbdecile=4;
  IF mb > DEC80 THEN mbdecile=5;
  if nmiss(mb) then mbdecile=.;
drop dec20 dec40 dec60 dec80 exchcd; proc sort; by cusip year;
data ivol; merge ivol opt; by cusip year;
proc sort; by year month mbdecile;
proc univariate data=ivol noprint;
var ivol; by year month mbdecile; where exchcd=1;
output out=decile pctlpts = 20 to 80 by 20 pctlpre=dec;
DATA ivol; MERGE ivol decile; BY year month mbdecile;
  pdecile=1;
  IF ivol > DEC20 THEN pdecile=2;
  IF ivol > DEC40 THEN pdecile=3;
  IF ivol > DEC60 THEN pdecile=4;
  IF ivol > DEC80 THEN pdecile=5;
  if nmiss(ivol) or nmiss(mb) then pdecile=.;
drop dec20 dec40 dec60 dec80;
proc sort data=ivol; by cusip year month;
data ivol; set ivol; lme=lag(me);
lcusip=lag(cusip); ivoldecile=lag(pdecile);
if lcusip ne cusip then ivoldecile=.;
if lcusip ne cusip then lme=.; proc means; run;
proc sort data=ivol nodupkey;
by cusip year month ivol;
proc tabulate data=ivol format=5.3;
class ivoldecile; var return;
table return, (ivoldecile all)*mean;
proc tabulate data=ivol format=5.3;
class mbdecile; var return;
table return, (mbdecile all)*mean; run;

options nosource nonotes errors=0;
proc sort data=ivol; by year month mbdecile ivoldecile;
proc means data=ivol noprint;
var return; by year month mbdecile ivoldecile;
weight lme; output out=factor mean(return) = port;
%macro a(many); %do i=1 %to 5; %do j=1 %to 5;
data factor&i&j; set factor; where ivoldecile=&i and mbdecile=&j;
port&i&j=port; drop port ivoldecile mbdecile;
run; %end; %end; %mend a; %a(1);
data temp.factor;
merge factor11 factor12 factor13 factor14 factor15
factor21 factor22 factor23 factor24 factor25
factor31 factor32 factor33 factor34 factor35
factor41 factor42 factor43 factor44 factor45
factor51 factor52 factor53 factor54 factor55;
by year month; if nmiss(month) then delete;
if (year=1963 and 1<month<7) or year>2018 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls"
DBMS=EXCEL REPLACE; SHEET="mbcondvw"; RUN;
rsubmit;
proc sort data=ivol; by year month mbdecile ivoldecile;
proc means data=ivol noprint;
var return; by year month mbdecile ivoldecile;
output out=factor mean(return) = port;
%macro a(many); %do i=1 %to 5; %do j=1 %to 5;
data factor&i&j; set factor; where ivoldecile=&i and mbdecile=&j;
port&i&j=port; drop port ivoldecile mbdecile;
run; %end; %end; %mend a; %a(1);
data temp.factor;
merge factor11 factor12 factor13 factor14 factor15
factor21 factor22 factor23 factor24 factor25
factor31 factor32 factor33 factor34 factor35
factor41 factor42 factor43 factor44 factor45
factor51 factor52 factor53 factor54 factor55;
by year month; if nmiss(month) then delete;
if (year=1963 and 1<month<7) or year>2018 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls"
DBMS=EXCEL REPLACE; SHEET="mbcondew"; RUN;

rsubmit;
options source notes errors=5;
data ivol; merge temp.crsp temp.ivol; by cusip year month;
data opt; set compf; year=year+1; proc sort nodupkey; by cusip year;
data cap; set temp.crsp (keep = cusip year month exchcd price);
where month=12; year=year+1; drop month; proc sort nodupkey; by cusip year;
data opt; merge cap opt; by cusip year; proc sort; by year; run;
proc univariate data=opt noprint; var mb; by year; where exchcd=1;
output out=decile pctlpts = 20 to 80 by 20 pctlpre=dec;
DATA opt; MERGE opt decile; BY year;
  mbdecile=1;
  IF mb > DEC20 THEN mbdecile=2;
  IF mb > DEC40 THEN mbdecile=3;
  IF mb > DEC60 THEN mbdecile=4;
  IF mb > DEC80 THEN mbdecile=5;
  if nmiss(mb) then mbdecile=.;
drop dec20 dec40 dec60 dec80 exchcd; proc sort; by cusip year;
data ivol; merge ivol opt; by cusip year;
proc sort; by year month mbdecile;
proc univariate data=ivol noprint;
var ivol; by year month mbdecile; where exchcd=1;
output out=decile pctlpts = 20 to 80 by 20 pctlpre=dec;
DATA ivol; MERGE ivol decile; BY year month mbdecile;
  pdecile=1;
  IF ivol > DEC20 THEN pdecile=2;
  IF ivol > DEC40 THEN pdecile=3;
  IF ivol > DEC60 THEN pdecile=4;
  IF ivol > DEC80 THEN pdecile=5;
  if nmiss(ivol) or nmiss(mb) then pdecile=.;
drop dec20 dec40 dec60 dec80;
proc sort data=ivol; by cusip year month;
data ivol; set ivol; lme=lag(me);
lcusip=lag(cusip); ivoldecile=lag(pdecile);
if lcusip ne cusip then ivoldecile=.;
if lcusip ne cusip then lme=.; proc means; run;
proc sort data=ivol nodupkey;
by cusip year month ivol;
proc tabulate data=ivol format=5.3;
class ivoldecile; var return;
table return, (ivoldecile all)*mean;
proc tabulate data=ivol format=5.3;
class mbdecile; var return;
table return, (mbdecile all)*mean; run;

options nosource nonotes errors=0;
proc sort data=ivol; by year month mbdecile ivoldecile;
proc means data=ivol noprint;
var return; by year month mbdecile ivoldecile;
weight lme; output out=factor mean(return) = port;
%macro a(many); %do i=1 %to 5; %do j=1 %to 5;
data factor&i&j; set factor; where ivoldecile=&i and mbdecile=&j;
port&i&j=port; drop port ivoldecile mbdecile;
run; %end; %end; %mend a; %a(1);
data temp.factor;
merge factor11 factor12 factor13 factor14 factor15
factor21 factor22 factor23 factor24 factor25
factor31 factor32 factor33 factor34 factor35
factor41 factor42 factor43 factor44 factor45
factor51 factor52 factor53 factor54 factor55;
by year month; if nmiss(month) then delete;
if year<1964 or year>2018 or (year=1964 and month=1) then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls"
DBMS=EXCEL REPLACE; SHEET="mbfcondvw"; RUN;
rsubmit;
proc sort data=ivol; by year month mbdecile ivoldecile;
proc means data=ivol noprint;
var return; by year month mbdecile ivoldecile;
output out=factor mean(return) = port;
%macro a(many); %do i=1 %to 5; %do j=1 %to 5;
data factor&i&j; set factor; where ivoldecile=&i and mbdecile=&j;
port&i&j=port; drop port ivoldecile mbdecile;
run; %end; %end; %mend a; %a(1);
data temp.factor;
merge factor11 factor12 factor13 factor14 factor15
factor21 factor22 factor23 factor24 factor25
factor31 factor32 factor33 factor34 factor35
factor41 factor42 factor43 factor44 factor45
factor51 factor52 factor53 factor54 factor55;
by year month; if nmiss(month) then delete;
if year<1964 or year>2018 or (year=1964 and month=1) then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls"
DBMS=EXCEL REPLACE; SHEET="mbfcondew"; RUN;

rsubmit;
options source notes errors=5;
data ivol; merge temp.crsp temp.ivol; by cusip year month;
data opt; set compann; year=year+1; proc sort nodupkey; by cusip year;
data cap; set temp.crsp (keep = cusip year month exchcd price);
where month=12; year=year+1; drop month; proc sort nodupkey; by cusip year;
data opt; merge cap opt; by cusip year; proc sort; by year; run;
proc univariate data=opt noprint; var mb; by year; where exchcd=1 and price ge 5;
output out=decile pctlpts = 20 to 80 by 20 pctlpre=dec;
DATA opt; MERGE opt decile; BY year;
  mbdecile=1;
  IF mb > DEC20 THEN mbdecile=2;
  IF mb > DEC40 THEN mbdecile=3;
  IF mb > DEC60 THEN mbdecile=4;
  IF mb > DEC80 THEN mbdecile=5;
  if nmiss(mb) or price<5 then mbdecile=.;
drop dec20 dec40 dec60 dec80 exchcd; proc sort; by cusip year;
data ivol; merge ivol opt; by cusip year;
proc sort; by year month mbdecile;
proc univariate data=ivol noprint; var ivol;
by year month mbdecile; where exchcd=1 and price ge 5;
output out=decile pctlpts = 20 to 80 by 20 pctlpre=dec;
DATA ivol; MERGE ivol decile; BY year month mbdecile;
  pdecile=1;
  IF ivol > DEC20 THEN pdecile=2;
  IF ivol > DEC40 THEN pdecile=3;
  IF ivol > DEC60 THEN pdecile=4;
  IF ivol > DEC80 THEN pdecile=5;
  if nmiss(ivol) or price<5 or nmiss(mb) then pdecile=.;
drop dec20 dec40 dec60 dec80;
proc sort data=ivol; by cusip year month;
data ivol; set ivol; lme=lag(me);
lcusip=lag(cusip); ivoldecile=lag(pdecile);
if lcusip ne cusip then ivoldecile=.;
if lcusip ne cusip then lme=.; proc means; run;
proc sort data=ivol nodupkey;
by cusip year month ivol;
proc tabulate data=ivol format=5.3;
class ivoldecile; var return;
table return, (ivoldecile all)*mean;
proc tabulate data=ivol format=5.3;
class mbdecile; var return;
table return, (mbdecile all)*mean; run;

options nosource nonotes errors=0;
proc sort data=ivol; by year month mbdecile ivoldecile;
proc means data=ivol noprint;
var return; by year month mbdecile ivoldecile;
weight lme; output out=factor mean(return) = port;
%macro a(many); %do i=1 %to 5; %do j=1 %to 5;
data factor&i&j; set factor; where ivoldecile=&i and mbdecile=&j;
port&i&j=port; drop port ivoldecile mbdecile;
run; %end; %end; %mend a; %a(1);
data temp.factor;
merge factor11 factor12 factor13 factor14 factor15
factor21 factor22 factor23 factor24 factor25
factor31 factor32 factor33 factor34 factor35
factor41 factor42 factor43 factor44 factor45
factor51 factor52 factor53 factor54 factor55;
by year month; if nmiss(month) then delete;
if (year=1963 and 1<month<7) or year>2018 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls"
DBMS=EXCEL REPLACE; SHEET="mbcondvw5"; RUN;
rsubmit;
proc sort data=ivol; by year month mbdecile ivoldecile;
proc means data=ivol noprint;
var return; by year month mbdecile ivoldecile;
output out=factor mean(return) = port;
%macro a(many); %do i=1 %to 5; %do j=1 %to 5;
data factor&i&j; set factor; where ivoldecile=&i and mbdecile=&j;
port&i&j=port; drop port ivoldecile mbdecile;
run; %end; %end; %mend a; %a(1);
data temp.factor;
merge factor11 factor12 factor13 factor14 factor15
factor21 factor22 factor23 factor24 factor25
factor31 factor32 factor33 factor34 factor35
factor41 factor42 factor43 factor44 factor45
factor51 factor52 factor53 factor54 factor55;
by year month; if nmiss(month) then delete;
if (year=1963 and 1<month<7) or year>2018 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls"
DBMS=EXCEL REPLACE; SHEET="mbcondew5"; RUN;

rsubmit;
options source notes errors=5;
data ivol; merge temp.crsp temp.ivol; by cusip year month;
data opt; set compf; year=year+1; proc sort nodupkey; by cusip year;
data cap; set temp.crsp (keep = cusip year month exchcd price);
where month=12; year=year+1; drop month; proc sort nodupkey; by cusip year;
data opt; merge cap opt; by cusip year; proc sort; by year; run;
proc univariate data=opt noprint; var mb; by year; where exchcd=1 and price ge 5;
output out=decile pctlpts = 20 to 80 by 20 pctlpre=dec;
DATA opt; MERGE opt decile; BY year;
  mbdecile=1;
  IF mb > DEC20 THEN mbdecile=2;
  IF mb > DEC40 THEN mbdecile=3;
  IF mb > DEC60 THEN mbdecile=4;
  IF mb > DEC80 THEN mbdecile=5;
  if nmiss(mb) or price<5 then mbdecile=.;
drop dec20 dec40 dec60 dec80 exchcd; proc sort; by cusip year;
data ivol; merge ivol opt; by cusip year;
proc sort; by year month mbdecile;
proc univariate data=ivol noprint; var ivol;
by year month mbdecile; where exchcd=1 and price ge 5;
output out=decile pctlpts = 20 to 80 by 20 pctlpre=dec;
DATA ivol; MERGE ivol decile; BY year month mbdecile;
  pdecile=1;
  IF ivol > DEC20 THEN pdecile=2;
  IF ivol > DEC40 THEN pdecile=3;
  IF ivol > DEC60 THEN pdecile=4;
  IF ivol > DEC80 THEN pdecile=5;
  if nmiss(ivol) or price<5 or nmiss(mb) then pdecile=.;
drop dec20 dec40 dec60 dec80;
proc sort data=ivol; by cusip year month;
data ivol; set ivol; lme=lag(me);
lcusip=lag(cusip); ivoldecile=lag(pdecile);
if lcusip ne cusip then ivoldecile=.;
if lcusip ne cusip then lme=.; proc means; run;
proc sort data=ivol nodupkey;
by cusip year month ivol;
proc tabulate data=ivol format=5.3;
class ivoldecile; var return;
table return, (ivoldecile all)*mean;
proc tabulate data=ivol format=5.3;
class mbdecile; var return;
table return, (mbdecile all)*mean; run;

options nosource nonotes errors=0;
proc sort data=ivol; by year month mbdecile ivoldecile;
proc means data=ivol noprint;
var return; by year month mbdecile ivoldecile;
weight lme; output out=factor mean(return) = port;
%macro a(many); %do i=1 %to 5; %do j=1 %to 5;
data factor&i&j; set factor; where ivoldecile=&i and mbdecile=&j;
port&i&j=port; drop port ivoldecile mbdecile;
run; %end; %end; %mend a; %a(1);
data temp.factor;
merge factor11 factor12 factor13 factor14 factor15
factor21 factor22 factor23 factor24 factor25
factor31 factor32 factor33 factor34 factor35
factor41 factor42 factor43 factor44 factor45
factor51 factor52 factor53 factor54 factor55;
by year month; if nmiss(month) then delete;
if year<1964 or year>2018 or (year=1964 and month=1) then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls"
DBMS=EXCEL REPLACE; SHEET="mbfcondvw5"; RUN;
rsubmit;
proc sort data=ivol; by year month mbdecile ivoldecile;
proc means data=ivol noprint;
var return; by year month mbdecile ivoldecile;
output out=factor mean(return) = port;
%macro a(many); %do i=1 %to 5; %do j=1 %to 5;
data factor&i&j; set factor; where ivoldecile=&i and mbdecile=&j;
port&i&j=port; drop port ivoldecile mbdecile;
run; %end; %end; %mend a; %a(1);
data temp.factor;
merge factor11 factor12 factor13 factor14 factor15
factor21 factor22 factor23 factor24 factor25
factor31 factor32 factor33 factor34 factor35
factor41 factor42 factor43 factor44 factor45
factor51 factor52 factor53 factor54 factor55;
by year month; if nmiss(month) then delete;
if year<1964 or year>2018 or (year=1964 and month=1) then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls"
DBMS=EXCEL REPLACE; SHEET="mbfcondew5"; RUN;
